package ar.com.muebleria.model.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import ar.com.muebleria.model.Conector;

public class DAOCombos {
	private Connection connection;
	public DAOCombos(){
		Conector conector = Conector.getInstance();
		connection = conector.getConnection();
	}

	public void close(){
		try {
			connection.close();
		} catch (SQLException e) {
			Logger.getLogger("DAOCombos").error(e);
		}
	}
	public ArrayList<String> obtenerCombo(String tabla, String id, String descripcion){
		ArrayList<String> out = new ArrayList<String>();
		
		String sqlSelect = "SELECT " + id + " ||' - '||" + descripcion + " as dato" +
				           "  FROM " + tabla + "" +
				           		" ORDER BY " + id + ";";
		
		Statement sentencia = null;
		try {
			Logger.getLogger("DAOCombos").info("Creando el statement.");
			sentencia = connection.createStatement();
		} catch (SQLException e) {
			Logger.getLogger("DAOCombos").error("Error al crear el Statemnet",e);
		}
		ResultSet rs = null;
		try {
			Logger.getLogger("DAOCombos").info("Creando resultSet de consulta.");
			Logger.getLogger("DAOCombos").info("Se ejecutara " + sqlSelect);
			rs = sentencia.executeQuery( sqlSelect );
		} catch (SQLException e) {
			Logger.getLogger("DAOCombos").error("Error en la ejecucion del query. " + sqlSelect,e);
		}
		try {
			while (rs.next()) {
				out.add(rs.getString("dato"));
//				dato
			}
		} catch (SQLException e) {
			Logger.getLogger("DAOCombos").error("Error al obtener los valores.",e);
		}finally{
			try {
				rs.close();
				sentencia.close();
			} catch (SQLException e) {
				Logger.getLogger("DAOProducto").error("No se cerro el Statement. ",e);

			}
		}
		
		return out;
	}

	public ArrayList<String> obtenerComboParametros(String tabla, String id, String descripcion,String parametros) {
		ArrayList<String> out = new ArrayList<String>();
		
		String sqlSelect = "SELECT " + id + " ||' - '||" + descripcion + " as dato" +
				           "  FROM " + tabla + " " +
				           " WHERE " + parametros + " " +
				           		" ORDER BY " + id + ";";
		
		Statement sentencia = null;
		try {
			Logger.getLogger("DAOCombos").info("Creando el statement.");
			sentencia = connection.createStatement();
		} catch (SQLException e) {
			Logger.getLogger("DAOCombos").error("Error al crear el Statemnet",e);
		}
		ResultSet rs = null;
		try {
			Logger.getLogger("DAOCombos").info("Creando resultSet de consulta.");
			Logger.getLogger("DAOCombos").info("Se ejecutara " + sqlSelect);
			rs = sentencia.executeQuery( sqlSelect );
		} catch (SQLException e) {
			Logger.getLogger("DAOCombos").error("Error en la ejecucion del query. " + sqlSelect,e);
		}
		try {
			while (rs.next()) {
				out.add(rs.getString("dato"));
//				dato
			}
		} catch (SQLException e) {
			Logger.getLogger("DAOCombos").error("Error al obtener los valores.",e);
		}finally{
			try {
				rs.close();
				sentencia.close();
			} catch (SQLException e) {
				Logger.getLogger("DAOProducto").error("No se cerro el Statement. ",e);

			}
		}
		
		return out;
	}
	
}
